package com.health.viewscontrols.medical;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.health.R;
import com.health.viewscontrols.medical.adapter.MedicalAdapter;
import com.health.viewscontrols.medical.base.BasePtActivity;
import com.health.viewscontrols.medical.base.DeviceEnum;
import com.health.viewscontrols.medical.bean.MedicalBean;
import com.nova.health.ble.BleConfigConstants;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

public class BleConfigActivity extends BasePtActivity {

    protected LinearLayout ll_finish;
    protected TextView tv_title;
    private RecyclerView rv_medical_config;
    private MedicalAdapter medicalAdapter;
    private List<DeviceEnum> deviceEnumList = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.activity_ble_config;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {

    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        ll_finish = contentView.findViewById(com.nova.health.ble.R.id.ll_finish);
        tv_title = contentView.findViewById(com.nova.health.ble.R.id.tv_title);
        rv_medical_config = contentView.findViewById(com.nova.health.ble.R.id.rv_medical_config);
        tv_title.setText("体检配置");
        ll_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeviceEnum.saveConfig(deviceEnumList);
                finish();
            }
        });
    }

    @Override
    public void doBusiness() {
        deviceEnumList = DeviceEnum.getConfig();
        initAdapter();
        medicalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DeviceEnum deviceEnum = deviceEnumList.get(position);
                deviceEnum.setShow(!deviceEnum.isShow());
                medicalAdapter.notifyItemChanged(position);
                DeviceEnum.saveConfig(deviceEnumList);
            }
        });
    }

    private void initAdapter() {
        medicalAdapter = new MedicalAdapter(R.layout.item_medical_list, deviceEnumList,true);
        rv_medical_config.setLayoutManager(new GridLayoutManager(this,4));
        rv_medical_config.setAdapter(medicalAdapter);
    }

    private void getConfig() {
        List<String> enumList = Hawk.get(BleConfigConstants.TJ_CONFIG_LIST, null);
        Log.e(TAG, "getConfig: " + enumList );
        deviceEnumList = DeviceEnum.getDeviceList();
        if (enumList == null) {
            //enumList == null 代表没有配置过，默认配置全部项目
            for (int i = 0; i < deviceEnumList.size(); i++) {
                deviceEnumList.get(i).setShow(true);
            }
            DeviceEnum.saveConfig(deviceEnumList);
        } else {
            //显示配置得项目
            for (int i = 0; i <deviceEnumList.size(); i++) {
                for (int j = 0; j < enumList.size(); j++) {
                    if (enumList.contains(deviceEnumList.get(i).getDeviceType())) {
                        deviceEnumList.get(i).setShow(true);
                    } else {
                        deviceEnumList.get(i).setShow(false);
                    }
                }
            }
        }
    }


    @Override
    public void onBackPressed() {
        DeviceEnum.saveConfig(deviceEnumList);
        super.onBackPressed();
    }
}
